<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends CI_Controller {
	
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 标列表
	 * @param $ftype 标类型
	 */
	function index(){
		//搜索参数
		$data['where']['qtype'] 		= '';
		$data['page_size']  			= BORROW_LIST_PAGE_SIZE;
		$data['page_start'] 			= 0;
		$data['select'] 				= 1;
		$this->load->model('Borrow_model');
		$resdata = $this->Borrow_model->get_borrow_blist($data);
		
		//获取用户信息
		$this->load->model('User_model');
		//$this->load->model('Credit_model');
		$borrow_list = array();
		//$areaList = $this->area->get_list();//地区表
		$borrowType	= $this->Borrow_model->arrays('borrow_type1');//标种
		$borrowStyle = $this->Borrow_model->arrays('borrow_style');//还款方式
		foreach ($resdata['borrow_list'] as $borrow){
			if ($borrow['ftype']!=6){
				$tmpBo = array();
				$user_obj = $this->User_model->get_by_uid($borrow['user_id']);
				$tmpBo['id'] = $borrow['id'];  //借款ID
				$tmpBo['title'] = $borrow['title']; //借款标题
				$tmpBo['user_id'] = $borrow['user_id']; //借款人用户ID
				$tmpBo['username'] = $user_obj['username'];//借款人用户名
				$tmpBo['ftype'] = $borrow['ftype'];//借款类型ID[4为流转标]
				$tmpBo['type_name']    = isset($borrowType[$borrow['ftype']]) ? $borrowType[$borrow['ftype']] : '';//借款类型
				$tmpBo['account'] = $borrow['account'];//借款金额
				$tmpBo['is_day'] = $borrow['is_day'];//是否天标,借款期限的单位[1以天为单位0以月为单位]
				$tmpBo['time_limit'] = $borrow['time_limit'];//借款期限[多少月,如果是天标则使用下面字段]
				$tmpBo['time_limit_day'] = $borrow['time_limit_day'];//借款期限[多少天,如果是月标则使用上面字段]
				$tmpBo['apr'] = $borrow['apr'];//借款年利率
				$tmpBo['funds'] = $borrow['funds'];//投标奖励百分比
				$tmpBo['account_yes'] = $borrow['account_yes'];//已借到金额 用于计算进度条
				$tmpBo['style']    = isset($borrowStyle[$borrow['style']]) ? $borrowStyle[$borrow['style']] : '';//还款方式
				//$tmpBo['province'] = isset($areaList[$user_obj['province']]) ?  $areaList[$user_obj['province']] : '';
				//$tmpBo['city'] 	= isset($areaList[$user_obj['city']]) ?  $areaList[$user_obj['city']] : '';
				//$tmpBo['area'] 	= isset($areaList[$user_obj['area']]) ?  $areaList[$user_obj['area']] : '';
				
				//$credit_obj = $this->Credit_model->get_row($user_id);
				//$credit_obj['credit_value'] = isset($credit_obj) ? $credit_obj['credit_value'] : 0;
				//$borrow['user_credit'] 		= $credit_obj['credit_value'];
				//$borrow['user_credit_img'] 	= $this->Credit_model->get_pic($credit_obj['credit_value']);
				$borrow_list[] 		= $tmpBo;
			}
		}
		
		$vdata['borrow_list']				= $borrow_list;
		$vdata['code'] = 0;
		echo json_encode($vdata);
	}
	
	//标详情
	function obj(){
		$id = $this->input->get_post('id');
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		//用户信息
		$user_obj 							= $this->User_model->get_by_uid($borrow_obj['user_id']);
		$borrowType	= $this->Borrow_model->arrays('borrow_type1');//标种
		$borrowStyle = $this->Borrow_model->arrays('borrow_style');//还款方式
		$tmpBo = array();
		$tmpBo['id'] = $borrow_obj['id'];//借款ID
		$tmpBo['title'] = $borrow_obj['title']; //借款标题
		$tmpBo['user_id'] = $borrow_obj['user_id']; //借款人用户ID
		$tmpBo['username'] = $user_obj['username'];//借款人用户名
		$tmpBo['ftype'] = $borrow_obj['ftype'];//借款类型ID[4为流转标]
		$tmpBo['type_name']    = isset($borrowType[$borrow_obj['ftype']]) ? $borrowType[$borrow_obj['ftype']] : '';//借款类型
		$tmpBo['account'] = $borrow_obj['account'];//借款金额
		$tmpBo['is_day'] = $borrow_obj['is_day'];//是否天标,借款期限的单位[1以天为单位0以月为单位]
		$tmpBo['time_limit'] = $borrow_obj['time_limit'];//借款期限[多少月,如果是天标则使用下面字段]
		$tmpBo['time_limit_day'] = $borrow['time_limit_day'];//借款期限[多少天,如果是月标则使用上面字段]
		$tmpBo['apr'] = $borrow_obj['apr'];//借款年利率
		$tmpBo['funds'] = $borrow_obj['funds'];//投标奖励百分比
		$tmpBo['account_yes'] = $borrow_obj['account_yes'];//已借到金额 用于计算进度条
		$tmpBo['style']    = isset($borrowStyle[$borrow_obj['style']]) ? $borrowStyle[$borrow_obj['style']] : '';//还款方式
		$tmpBo['verify_time'] = date('Y-m-d H:i:s',$borrow_obj['verify_time']);//发布时间
		$tmpBo['lowest_account'] = $borrow_obj['lowest_account'];//最低投标金额[流转标则是每份金额]
		$tmpBo['content'] = $borrow_obj['content'];//借款详情
		$vdata['borrow'] = $tmpBo;
		$vdata['code'] = 0;
		echo json_encode($vdata);
	}
	
	//投标记录
	function tender_list(){
		$id = $this->input->get_post('id');
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('where'=>array('borrow_id'=>$id)));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			
			$tmpT = array();
			$tmpT['username'] = $user_obj['username'];//投标人用户名
			$tmpT['money'] = $tender['money'];//投标金额
			$tmpT['account'] = $tender['account'];//有效金额
			$tmpT['add_time'] = date('Y-m-d H:i:s',$tender['add_time']);;//投标时间
			$borrow_tender_list1[] 		= $tmpT;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		$vdata['code'] = 0;
		echo json_encode($vdata);
	}
	
	//投标页
	function tender_view(){
		$id = $this->input->get_post('id');
		//$user = $this->session->userdata('ych_user');//获取用户session信息
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if(!$user_id || $token!=$token_val){//如果未登录就跳到登录页面
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
	
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		$user = $this->User_model->get_by_uid($user_id);//$this->session->userdata('ych_user');
		$user_account = array();
		if(!$user){
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}else{
			$user_account 					= $this->Account_model->get_account($user['user_id']);
			if($user_account == false){
				$user_account['total'] 		= 0;
				$user_account['use_money'] 	= 0;
			}
		}
		
		$retUser = array(
			'user_id' => $user['user_id'],
			'username' => $user['username'],
			'paypassword' => $user['paypassword'],
			'use_money' => $user_account['use_money'],//账户可用资金
		);
		$vdata['tuser_account']			= $retUser;//投资人信息
		
		$tmpBo = array();
		$tmpBo['id'] = $borrow_obj['id'];//标ID
		$tmpBo['title'] = $borrow_obj['title'];//借款标题
		$tmpBo['dxb_pwd'] = $borrow_obj['dxb_pwd'];//定向标密码[定向标需要密码才能投标,没有则不需要]
		$tmpBo['account'] = $borrow_obj['account'];//借款总额
		$tmpBo['account_yes'] = $borrow_obj['account_yes'];//已借到金额
		$tmpBo['lowest_account'] = $borrow_obj['lowest_account'];//最低投标金额
		
		$vdata['borrow'] = $tmpBo;//标信息
		$vdata['code'] = 0;
		echo json_encode($vdata);
	}
	
	//流转标投标页
	function lz_tender_view(){
		$id = $this->input->get_post('id');
		//$user = $this->session->userdata('ych_user');//获取用户session信息
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if(!$user_id || $token!=$token_val){//如果未登录就跳到登录页面
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
	
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		//$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$user = $this->User_model->get_by_uid($user_id);//$this->session->userdata('ych_user');
		if(!$user){
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
		$user_account = array();
		$user_account 					= $this->Account_model->get_account($user['user_id']);
		if($user_account == false){
			$user_account['total'] 		= 0;
			$user_account['use_money'] 	= 0;

		}
		$retUser = array(
			'user_id' => $user['user_id'],
			'username' => $user['username'],
			'paypassword' => $user['paypassword'],
			'use_money' => $user_account['use_money'],//账户可用资金
		);
		$vdata['tuser_account']			= $retUser;//投资人信息
	
		//借款人信息
		/*$user_obj = $this->User_model->get_by_uid($borrow_obj['user_id']);
		$retBorrowUser = array(
			'user_id' => $user_obj['user_id'],
			'username' => $user_obj['username'],
			'email' => $user['email'],
		);*/
		
		$tmpBo = array();
		$tmpBo['id'] = $borrow_obj['id'];//标ID
		$tmpBo['title'] = $borrow_obj['title'];//借款标题
		$tmpBo['dxb_pwd'] = $borrow_obj['dxb_pwd'];//定向标密码[定向标需要密码才能投标,没有则不需要]
		$tmpBo['account'] = $borrow_obj['account'];//借款总额
		$tmpBo['account_yes'] = $borrow_obj['account_yes'];//已借到金额
		$tmpBo['lowest_account'] = $borrow_obj['lowest_account'];//最低投标金额
		
		$vdata['borrow'] = $tmpBo;//标信息
		//$vdata['user_obj'] = $retBorrowUser;
		$vdata['title'] = $borrow_obj['title'].' - '.CON_KEYWORDS;
		//$this->load->view('mobile/borrow_lz_tender', $vdata);
		$vdata['code'] = 0;
		echo json_encode($vdata);
	}
	
	
	/**
	 * 投标
	 */
	function tender(){
		$ret['msg'] = '';
		$ret['code'] = 0;
		$id 				= $this->input->get_post("id");
		$account 			= $this->input->get_post("account");
		$paypassword 		= $this->input->get_post("paypassword");
		$dxb_pwd 			= $this->input->get_post("dxb_pwd")?$this->input->get_post("dxb_pwd"):"";
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if(!$user_id || $token!=$token_val){
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		
		$user = $this->User_model->get_by_uid($user_id);
		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['code'] = 2;
			echo json_encode($ret);exit;
		}
		if(!$this->myformcheck->IsDouble($account)){
			$ret['msg'] = "投标金额不正确";
			$ret['code'] = 3;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['fstatus'] != 1){
			$ret['msg'] = "此标不能投";
			$ret['code'] = 4;
			echo json_encode($ret);exit;
		}
		if((time()-$borrow_obj['verify_time']) > ($borrow_obj['valid_time']*3600*24)){
			$ret['msg'] = "此标已过期";
			$ret['code'] = 5;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['code'] = 6;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['lowest_account'] > $account){
			$ret['msg'] = "请输入正确的金额";
			$ret['code'] = 7;
			echo json_encode($ret);exit;
		}
		if(($borrow_obj['most_account'] > 0) && ($account > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['code'] = 8;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['code'] = 9;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['code'] = 10;
			echo json_encode($ret);exit;
		}
		$user_tender_all = $this->Borrow_model->get_borrow_tender(array('where'=>array('user_id'=>$user['user_id'],'borrow_id'=>$id)));
		$user_tender_account = 0;
		foreach($user_tender_all as $tender_obj){
			$user_tender_account += $tender_obj['account'];	
		}
		if(($borrow_obj['most_account'] > 0) && (($account+$user_tender_account) > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['code'] = 8;
			echo json_encode($ret);exit;
		}
		
		//封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		$data['borrow_id'] 	= $id;
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($account > $user_account['use_money']){
			$ret['msg'] = "您的余额不足,请先充值";
			$ret['code'] = 11;
			echo json_encode($ret);exit;
		}
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		$data['account'] 	= $account;
		
		//投标
		$flag = $this->Borrow_model->borrow_tender($data);
		if($flag == 1){
			if($borrow_obj['ftype'] == 5){//是否秒标
				$borrow_obj1 = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
				if($borrow_obj1['account'] == $borrow_obj1['account_yes']){//是否满标
					//执行复审操作
					$data['id']					= $id;
			  		$data['ip']					= $this->input->ip_address();
			  		$data["fstatus"]			= 3;
			  		$data["repayment_remark"]	= "秒标自动复审";
			  		$data["repayment_user"]		= 1;
		  			$flag = $this->Borrow_model->borrow_review_true($data);
		  			
		  			//执行还款操作
		  			$repayments = $this->Borrow_model->get_repayment_list(array('select'=>1,'where'=>array('borrow_id'=>$id, 'fstatus'=>0)));
		  			foreach($repayments['repayment_list'] as $repayment){
		  				$data['repayment_id'] 	= $repayment['id'];
						$data['user_id'] 		= $repayment['user_id'];
						$data['ip']	 			= $this->input->ip_address();
						$this->Borrow_model->repay($data);
		  			}
				}
			}
			$ret['msg'] = "投标成功";
			$ret['code'] = 0;
			echo json_encode($ret);exit;
		}else{
			$ret['msg'] = "投标失败";
			$ret['code'] = 12;
			echo json_encode($ret);exit;
		}
	}
	
	/**
	 * 流转标投标
	 */
	function lz_tender(){
		$ret['msg'] = '';
		$ret['code'] = 0;
		$id 				= $this->input->get_post("id");
		$money 				= $this->input->get_post("lz_account");
		$paypassword 		= $this->input->get_post("paypassword");
		$dxb_pwd 			= $this->input->get_post("dxb_pwd1")?$this->input->get_post("dxb_pwd1"):"";
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if(!$user_id || $token!=$token_val){
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
		$this->load->model('User_model');
		$user = $this->User_model->get_by_uid($user_id);
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['code'] = 2;
			echo json_encode($ret);exit;
		}
		if(!$this->myformcheck->isNumber($money)){
			$ret['msg'] = "份额不正确";
			$ret['code'] = 3;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['code'] = 4;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['code'] = 5;
			echo json_encode($ret);exit;
		}
		if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['code'] = 6;
			echo json_encode($ret);exit;
		}
		$account = $money*$borrow_obj['lowest_account'];
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_id'] 	= $id;
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($user_account['use_money'] < $account){
			$ret['msg'] = "您的余额不足，请先充值";
			$ret['code'] = 7;
			echo json_encode($ret);exit;
		}
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		$data['account'] 	= $account;
		
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		
		$flag = $this->Borrow_model->borrow_lz_tender($data);
		$ret['msg'] = "投标成功";
		$ret['code'] = 0;
		echo json_encode($ret);exit;
	}
}